package kr.ac.kaist.simplefileexplorer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class AddRecipientActivity extends Activity{
	ArrayList<ContactInfoItem> contactList; 
	private ListView listViewRest;
	
	Context ctx;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_general_list_with_button);
		
		ctx = this;
		
		contactList = new ArrayList<ContactInfoItem>();
		
        // Build adapter with contact entries
        Cursor cursor = getContacts();

        if (cursor.moveToFirst()) {
        	do{
	        	String phonenumber = cursor.getString(1).replaceAll("-","");

				ContactInfoItem acontact = new ContactInfoItem();
				acontact.setPhotoId(cursor.getLong(0));
				acontact.setPhoneNumber(phonenumber);
				acontact.setName(cursor.getString(2));
				
				contactList.add(acontact);
        	}while(cursor.moveToNext());
        	
        	listViewRest = (ListView)findViewById(R.id.general_list_listv); 
        	listViewRest.setAdapter(new AddRecipientAdapter(contactList, ctx));
        	listViewRest.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        	listViewRest.setItemsCanFocus(false);        	
        	
        	
        	listViewRest.setOnItemClickListener(new OnItemClickListener() {
      	      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      	    	  
      	    	  
      	      }
        	});	          
        }

	}
	
    /**
     * Obtains the contact list for the currently selected account.
     *
     * @return A cursor for for accessing the contact list.
     */
    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projections = new String[]{
        	    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
        	    ContactsContract.CommonDataKinds.Phone.NUMBER,
        	    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
        	  };

        String[] selectionArgs = null;
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + "1" + "'";        
        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";

        Cursor contactCursor = getContentResolver().query(uri, projections, null, selectionArgs, sortOrder);
        
        return contactCursor;
    }	
}



